import { defineStore } from "pinia";
import { ElMessage } from "element-plus";

import { Comment, CommentType, OperateType } from "@/types/comment";
import { CDComment } from "@/api/comment";

const useCommentStore = defineStore({
  id: "comment",
  state: () => ({
    replyMode: false, // 是否是评论模式，变成此模式后退格就会退出
    replyData: null as null | Comment, // 回复评论的id
    replyContent: "",
  }),
  actions: {
    setReplyContent(content: string) {
      this.replyContent = content;
    },
    setReplyData(data: Comment) {
      this.replyData = data;
    },
    setReplyMode(payload: boolean) {
      this.replyMode = payload;
    },
    addComment: async (params: {
      type: Number;
      id: number | string;
      content: string;
    }) => {
      const res = await CDComment({
        t: OperateType["发送"],
        ...params,
      });
      if (res && res.code === 200) {
        ElMessage.success("操作成功");
      }
    },

    replyComment: async (params: {
      type: Number;
      id: number | string;
      content: string;
      commentId: string | number;
    }) => {
      const res = await CDComment({
        t: OperateType["回复"],
        ...params,
      });
      if (res && res.code === 200) {
        ElMessage.success("操作成功");
      }
    },

    deleteComment: async (params: {
      type: Number;
      id: number | string;

      commentId: string | number;
    }) => {
      const res = await CDComment({
        t: OperateType["删除"],
        ...params,
      });
      if (res && res.code === 200) {
        ElMessage.success("操作成功");
      }
    },
  },
});

export default useCommentStore;
